package zxj.spay.modular.report.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import zxj.spay.entity.Type;
import zxj.spay.modular.data.dao.StreamDao;
import zxj.spay.modular.data.dao.TypeDao;
import zxj.spay.modular.report.bean.IndexTotal;
import zxj.spay.modular.report.service.ReportService;
import zxj.spay.util.DateUtil;

@Service
public class ReportServiceImpl implements ReportService{
	@Resource
	private StreamDao streamDao;
	@Resource
	private TypeDao typeDao;

	@Override
	public Map<String, IndexTotal> getIndexListTotal(int user_id) {
		//获取收支	日
		IndexTotal day = getIndexDayTotal(user_id);
		//获取收支	周
		IndexTotal week = getIndexWeekTotal(user_id);
		//获取收支	月
		IndexTotal month = getIndexMonthTotal(user_id);
		//获取收支	年
		IndexTotal year = getIndexYearTotal(user_id);
		
		Map<String,IndexTotal> data = new HashMap<>();
		data.put("day", day);
		data.put("week", week);
		data.put("month", month);
		data.put("year", year);
		return data;
	}

	/**
	 * 获取本年的收支统计
	 * @param user_id 用户id
	 * @return
	 */
	private IndexTotal getIndexYearTotal(int user_id) {
		Date start = DateUtil.getThisYearFirstDay();
		Date end = new Date();
		//收入
		Integer income = streamDao.getIndexTotalByTimeAndUserId(user_id, Type.STATUS_INCOME, start, end);
		//支出
		Integer cost = streamDao.getIndexTotalByTimeAndUserId(user_id, Type.STATUS_COST, start, end);
		
		IndexTotal total = new IndexTotal();
		total.setCost(countMoney(cost));
		total.setIncome(countMoney(income));
		return total;
	}

	/**
	 * 获取本月的收支统计
	 * @param user_id 用户id
	 * @return
	 */
	private IndexTotal getIndexMonthTotal(int user_id) {
		Date start = DateUtil.getThisMonthFirstDay();
		Date end = new Date();
		//收入
		Integer income = streamDao.getIndexTotalByTimeAndUserId(user_id, Type.STATUS_INCOME, start, end);
		//支出
		Integer cost = streamDao.getIndexTotalByTimeAndUserId(user_id, Type.STATUS_COST, start, end);
		
		IndexTotal total = new IndexTotal();
		total.setCost(countMoney(cost));
		total.setIncome(countMoney(income));
		return total;
	}

	/**
	 * 获取本周的收支统计
	 * @param user_id 用户id
	 * @return
	 */
	private IndexTotal getIndexWeekTotal(int user_id) {
		Date start = DateUtil.getThisWeekMonday();
		Date end = DateUtil.getThisWeekSunday();
		//收入
		Integer income = streamDao.getIndexTotalByTimeAndUserId(user_id, Type.STATUS_INCOME, start, end);
		//支出
		Integer cost = streamDao.getIndexTotalByTimeAndUserId(user_id, Type.STATUS_COST, start, end);
		
		IndexTotal total = new IndexTotal();
		total.setCost(countMoney(cost));
		total.setIncome(countMoney(income));
		return total;
	}

	/**
	 * 获取本日的收支统计
	 * @return
	 */
	private IndexTotal getIndexDayTotal(int user_id) {
		Date date = new Date();
		Date start = DateUtil.getTheDayFirstTimeToDate(date);
		Date end = DateUtil.getTheDayLastTimeToDate(date);
		//收入
		Integer income =  streamDao.getIndexTotalByTimeAndUserId(user_id,Type.STATUS_INCOME,start,end);
		//支出
		Integer cost =  streamDao.getIndexTotalByTimeAndUserId(user_id,Type.STATUS_COST,start,end);
		
		IndexTotal total = new IndexTotal();
		total.setCost(countMoney(cost));
		total.setIncome(countMoney(income));
		return total;
	}
	
	
	/**
	 * 把分转换为元
	 * @param money
	 * @return
	 */
	public double countMoney(Integer money){
		money = null == money ? 0 : money;
		BigDecimal dincome = new BigDecimal(money).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP);
		
		return dincome.doubleValue();
	}

	@Override
	public List<Map<String, Object>> getThisMonthCostAreaTotal(int user_id) {
		Date start = DateUtil.getThisMonthFirstDay();
		Date end = new Date();
		List<Map<String, Object>> costTotal = streamDao.getCostTotal(user_id, start, end);
		Iterator<Map<String, Object>> iterator = costTotal.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			int money = Integer.parseInt(next.get("money").toString());
			double countMoney = countMoney(money);
			next.put("money", countMoney);
		}
		return costTotal;
	}

	@Override
	public List<Map<String, Object>> getThisMonthCostDayTotal(int user_id) {
		Date start = DateUtil.getThisMonthFirstDay();
		Date end = new Date();
		List<Map<String,Object>> data = streamDao.getCostDayTotal(user_id,Type.STATUS_COST,start,end);
		data = ConvertMoney(data);
		//把中间没有数据的天数，填充0
		List<Map<String, Object>> nullDayToZero = nullDayToZero(data);
		return nullDayToZero;
	}
	
	@Override
	public List<Map<String, Object>> getThisMonthIncomeDayTotal(int id) {
		Date start = DateUtil.getThisMonthFirstDay();
		Date end = new Date();
		List<Map<String,Object>> data = streamDao.getCostDayTotal(id,Type.STATUS_INCOME,start,end);
		data = ConvertMoney(data);
		//把中间没有数据的天数，填充0
		List<Map<String, Object>> nullDayToZero = nullDayToZero(data);
		return nullDayToZero;
	}
	
	private List<Map<String, Object>> ConvertMoney(List<Map<String, Object>> data){
		Iterator<Map<String, Object>> iterator = data.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object object = next.get("money");
			int parseInt = Integer.parseInt(object.toString());
			double countMoney = countMoney(parseInt);
			next.put("money", countMoney);
		}
		return data;
	}
	
	/**
	 * 处理一个日期，把没有日期数据，填充为0
	 * @param data
	 * @return
	 */
/*	@SuppressWarnings("deprecation")
	public List<Map<String,Object>> nullDayToZero(List<Map<String,Object>> data){
		Calendar calendar = Calendar.getInstance();
		int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		calendar.add(Calendar.DATE, -day_of_month + 1);
		int size = data.size();
		for(int i = 0 ; i < size ; i ++){
			String time = data.get(i).get("useTime").toString();
			Date parseDate = DateUtil.parseDate(time, "yyyy-MM-dd HH:mm:ss.s");
			Date orderDate = calendar.getTime();
			calendar.add(Calendar.DATE, 1);
			if(parseDate.getTime() == orderDate.getTime()){
				continue;
			}else{
				Map<String,Object> zero = new HashMap<>();
				zero.put("money", 0);
				zero.put("useTime", orderDate);
				data.add(i, zero);
				size++;
			}
			if(size == day_of_month){
				break;
			}
		}
		return data;
	}*/
	
	@SuppressWarnings("deprecation")
	public List<Map<String,Object>> nullDayToZero(List<Map<String,Object>> data){
		Calendar calendar = Calendar.getInstance();
		int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		calendar.add(Calendar.DATE, -day_of_month + 1);
		for(int i = 0 ; i < day_of_month ; i ++){
			Date orderDate = calendar.getTime();
			if(i == data.size()){
				if(i == 21){
					System.out.println();
				}
				//数据列表为空
				Map<String,Object> zero = new HashMap<>();
				zero.put("money", 0);
				zero.put("useTime", orderDate);
				data.add(i, zero);
				calendar.add(Calendar.DATE, 1);
				continue;
			}
			calendar.add(Calendar.DATE, 1);
			String time = data.get(i).get("useTime").toString();
			Date parseDate = DateUtil.parseDate(time, "yyyy-MM-dd HH:mm:ss.s");
			if(parseDate.getTime() == orderDate.getTime()){
				continue;
			}else{
				Map<String,Object> zero = new HashMap<>();
				zero.put("money", 0);
				zero.put("useTime", orderDate);
				data.add(i, zero);
			}
		}
		return data;
	}



}
